<template>
  <div class="container">
    <div class="loginBox">
      <img :src="src" alt="" />
      <h1>新冠物资管理系统</h1>
      <el-form
        :model="user"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import { login } from "@/api/user";
  import { mapMutations  } from 'vuex';// 从vuex中导入mapState
  import { USERLOGIN, USERLOGOUT } from "@/store/types";
  export default {
  data() {
    return {
      src: "https://cdn.cnbj1.fds.api.mi-img.com/mi.com-assets/shop/img/logo-mi2.png",
      user: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    ...mapMutations([USERLOGIN,USERLOGOUT]),
    submitForm(formName) {
      //这里就是要去执行的正则的校验

      var $this=this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let appid = '197078301'; // 腾讯云控制台中对应这个项目的 appid
          //生成一个滑块验证码对象
          let captcha = new TencentCaptcha(appid,async res => {
            // 用户滑动结束或者关闭弹窗，腾讯返回的内容
            //console.log(res)
            if (res.ret === 0) {
              let result = await login(
                      $this.user.username,
                      $this.user.password);
              let token = result.data.token;
              $this[USERLOGIN](token);
              $this.$router.push("/home");
            } else {
              // 提示用户完成验证
            }
          });
          // 滑块显示
          captcha.show();
          //通过编程式路由跳转到主页面
          //this.$router.push("/home");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
    computed: {},
};
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: aquamarine;

  text-align: center;

  img {
    width: 60px;
    height: 60px;
  }
}
.loginBox {
  width: 40%;
  height: 300px;
  background-color: #ffffff;
  border: 1px solid #dcdfe6;
  border-radius: 2px;
  padding: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
}
</style>